package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.inspection.AssessmentCimModel;
import com.sduept.bigdata.equipment.assessment.service.AssessmentCimModelService;
import com.sduept.bigdata.equipment.assessment.service.AssessmentCimService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.List;

/**
 * 检查设备维护，提供增删改查等业务方法
 * @author huangyu
 * @Date 2019年11月21日
 */
@Named
@ViewScoped
public class InspectionEquipmentMaintenanceController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8600766131154881564L;
	
	@Autowired
	private AssessmentCimModelService service;
	@Autowired
	private AssessmentCimService assessmentCimService;
	
	private TreeNode root;
	
	private AssessmentCimModel create;
	
	private AssessmentCimModel selected = null;
	
	@PostConstruct
	public void init() {
		initTreeTable();
	}

	private void initTreeTable() {
		root = new LibTreeNode("Root", null);
		List<AssessmentCimModel> ps = service.findPidIsNull();
		for (AssessmentCimModel p : ps) {
			new LibTreeNode(p, root);
		}
		root.setExpanded(true);
	}
	
	public void preCreate() {
		create = new AssessmentCimModel();
		if(null != selected) {
			create.setParent(selected);
		}
	}
	
	public void preEdit() {
		create = selected;
	}
	
	public void save() {
		service.createOrUpdate(create);
		addSuccessMessage("保存成功");
		selected = null;
		create = null;
		initTreeTable();
		assessmentCimService.reload();
	}
	
	public void delete() {
		try {
			service.deleteById(selected.getId());
			addSuccessMessage("删除成功");
			selected = null;
			initTreeTable();
			assessmentCimService.reload();
		} catch (Exception e) {
			addErrorMessage("删除失败");
		}
	}
	
	public void onNodeExpand(NodeExpandEvent event) {
		TreeNode node = event.getTreeNode();
		AssessmentCimModel pData = (AssessmentCimModel) node.getData();
		List<AssessmentCimModel> childs = service.findByPid(pData.getId());
		for (AssessmentCimModel c : childs) {
			new LibTreeNode(c, node);
		}
	}
	
	public void onNodeSelect(NodeSelectEvent event) {
		TreeNode node = event.getTreeNode();
		selected = (AssessmentCimModel) node.getData();
	}

	public TreeNode getRoot() {
		return root;
	}

	public AssessmentCimModel getCreate() {
		return create;
	}

	public AssessmentCimModel getSelected() {
		return selected;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public void setCreate(AssessmentCimModel create) {
		this.create = create;
	}

	public void setSelected(AssessmentCimModel selected) {
		this.selected = selected;
	}
	
}
